const express = require("express");
let data = require("./data.json");
// let todoList = require("./todoList.json")
const fs = require("fs");
const { log } = require("console");
let app = express();

app.get("/getmovies", (req, res) => {
  // console.log(data);
  res.json(data)
})

app.get("/searchMovie", (req, res) => {
  // 接收传入的搜索值；
  let { searchMovie } = req.query;
  console.log(searchMovie)
  // let reg = /searchMovie/g;
  let reg = new RegExp(searchMovie);

  let resdata = data.subjects.filter(item => reg.test(item.title));

  res.json(resdata);
})

// todolist

app.get("/gettodoList", (req, res) => {
  console.log("111");
  res.json(todoList)
})

// 存储保存数据
app.get("/storeList", (req, res) => {
  // console.log("111");
  let { inpValue } = res.query
  console.log(inpValue);
  try {
    fs.writeFileSync("./todoList.json", JSON.stringify(inpValue));
    res.json({
      info: "存储成功",
      status: 1
    })
  } catch (err) {
    res.json({
      info: "存储失败",
      status: 0,
      err
    })
  }

})
app.listen(8989);